package xyz.molzhao.alink.exception;


import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.molzhao.alink.base.Result;
import xyz.molzhao.alink.base.ResultEnum;

import javax.validation.ValidationException;
import java.nio.file.AccessDeniedException;
import java.sql.SQLException;


/**
 * @ClassName DefaultExceptionAdvice
 * @Description 默认异常处理
 * @Author molzhao
 * @Date 2021/8/18 10:25
 * @Version v1.0
 */
@RestControllerAdvice
@Slf4j
public class DefaultExceptionAdvice {
    /**
     * IllegalArgumentException异常处理返回json
     * 返回状态码:400
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({IllegalArgumentException.class})
    public <T> Result<T> badRequestException(IllegalArgumentException e) {
        return defHandler("参数解析失败", e);
    }

    /**
     * AccessDeniedException异常处理返回json
     * 返回状态码:403
     */
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler({AccessDeniedException.class})
    public <T> Result<T> badMethodExpressException(AccessDeniedException e) {
        return defHandler("没有权限请求当前方法", e);
    }

    /**
     * 返回状态码:405
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public <T> Result<T> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return defHandler("不支持当前请求方法", e);
    }

    /**
     * 返回状态码:415
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler({HttpMediaTypeNotSupportedException.class})
    public <T> Result<T> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        return defHandler("不支持当前媒体类型", e);
    }

    /**
     * SQLException sql异常处理
     * 返回状态码:500
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({SQLException.class})
    public <T> Result<T> handleSQLException(SQLException e) {
        return defHandler("服务运行SQLException异常", e);
    }


    @ExceptionHandler(BusinessException.class)
    public <T> Result<T> handBusinessException(BusinessException be) {
        log.warn("业务异常: {}",be.getMessage(), be);
        return Result.failed(be);
    }

    @ExceptionHandler(ValidationException.class)
    public <T> Result<T> handValidationException(ValidationException ve) {
        return defHandler(ResultEnum.PARAM_ERROR, ve);
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result<String> handMissingServletRequestParameterException(MissingServletRequestParameterException me) {
        return defHandler(ResultEnum.PARAM_ERROR, me);
    }

    /**
     * 所有异常统一处理
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public <T> Result<T> handleException(Exception e) {
        return defHandler("未知异常", e);
    }

    private <T> Result<T> defHandler(String msg, Exception e) {
        log.error(msg, e);
        return Result.failed(msg);
    }

    private <T> Result<T> defHandler(ResultEnum result, Exception e) {
        log.error(result.getDescription(), e);
        return Result.failed(result);
    }
}
